import { initState } from "./initSate";
import { compileToFunction } from './compile/index';
import { mounteComponent } from "./vnode/lifecycle";
import { mergeOptions } from "./utils/index";
import { callHook } from "./vnode/lifecycle";

export function initMixin(Vue) {
  Vue.prototype._init = function (options) {
    let vm = this;
    vm.$options = mergeOptions(Vue.options, options);
    callHook(vm, 'beforeCreate');
    // 状态初始化
    initState(vm);
    callHook(vm, 'created');
    // 渲染模板 el
    if (vm.$options.el) {
      vm.$mount(vm.$options.el)
    }
  }
  // 创建 $mounted
  Vue.prototype.$mount = function (el) {
    let vm = this;
    let options = vm.$options;
    el = document.querySelector(el);
    vm.$el = el;
    if (!options.render) {
      let template = options.template;
      if (!template && el) {
        // 获取html
        el = el.outerHTML;
        // 变成render函数
        let render = compileToFunction(el);
        // 将render变成vnode， 将vnode转化为真实dom，放到页面上去
        options.render = render;
      }
    }
    // 挂载组件
    mounteComponent(vm, el);
  }
}


